package cn.niit.controller;

import cn.niit.entity.Classes;
import cn.niit.entity.Course;
import cn.niit.entity.CourseTime;
import cn.niit.service.ClassService;
import cn.niit.service.CourseService;
import cn.niit.service.CourseTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/courseTime")
public class CourseTimeController {
    @Resource
    private CourseTimeService courseTimeService;

    @Autowired
    private ClassService classService;

    @Autowired
    private CourseService courseService;

    @GetMapping("/select")
    public String select(Model model, CourseTime courseTime) {
//        List<CourseTime> select = courseTimeService.select(courseTime);
//        List<CourseTime> select = courseTimeService.queryByCourseTimeList(courseTime);
//        model.addAttribute("courseTime", select);
        return this.queryByCourseTimeList(model, courseTime);
//        return "/course/courseTimeList";
    }

    /**
     * 初始化管理员侧的课程表页面
     * @param model
     * @param courseTime
     * @return
     */
    @GetMapping("/queryByCourseTimeList")
    public String queryByCourseTimeList(Model model, CourseTime courseTime) {
        /**
         * TODO 实现思路：
         * 1、初始化查询条件：班级列表，用于查询条件中以下拉框显示；
         * 2、根据班级列表中的第一个班级编号（classCode）查询该班级课程表；
         */
        List<Classes> classesList = classService.queryAllClass();
        Integer classCode = 0;
        if(null != classesList && !classesList.isEmpty()){
            model.addAttribute("classesList", classesList);

            classCode = classesList.get(0).getClassCode();
            if(null == courseTime || null == courseTime.getClassCode() || courseTime.getClassCode() <= 0){
                courseTime.setClassCode(classCode);
            }
            List<CourseTime> queryByCourseTimeList = courseTimeService.queryByCourseTimeList(courseTime);
            model.addAttribute("courseTimeList", queryByCourseTimeList);
            model.addAttribute("courseTime", courseTime);
        }
        return "/course/courseTimeList";
    }

//    @GetMapping("/query")
//    public String queryByCourseTime(Model model, CourseTime courseTime) {
//        List<CourseTime> queryByCourseTime = courseTimeService.queryByCourseTime(courseTime);
////        String userCode = (String) SecurityUtils.getSubject().getPrincipal();
////        int classCode = 1;
////        courseTime.setClassCode(classCode);
//        model.addAttribute("courseTime", queryByCourseTime);
//        return "/course/courseTime";
//    }



    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable("id") Integer id, Model model) {
        List<Course> courseList=courseService.queryAllCourseList();



        model.addAttribute("courseList",courseList);
        model.addAttribute("courseTime", courseTimeService.queryCourseTimeById(id));
        return "/course/editCourseTime";
    }

    @PostMapping("/edit")
    public String edit(CourseTime courseTime) {


        CourseTime courseTime1 = new CourseTime();
        Integer id = courseTime.getId();
        courseTime1.setCourseCode(courseTime.getCourseCode());
        List<CourseTime> courseTimes = courseTimeService.queryByCourseCode(courseTime);
if (id!=null) {
    boolean b = courseTimeService.editCourseTimeList(courseTime);
}else{
    boolean b=courseTimeService.addCourseTimeList(courseTime);
}
        return "redirect:/courseTime/queryByCourseTimeList";
    }

//    @GetMapping("/update")
//    public String update(Model model) {
//        CourseTime courseTime = new CourseTime();
//        model.addAttribute("courseTime", courseTime);
//        return "/course/editCourseTime";
//    }

    /**
     * /addCourseTime/1/3
     * @param model
     * @param section
     * @param week
     * @return
     */
    @GetMapping("/addCourseTime/{classCode}/{section}/{week}")
    public String addCourseTime(Model model, @PathVariable("classCode") Integer classCode,
                                @PathVariable("section") Integer section, @PathVariable("week") Integer week) {
        /**
         * TODO 实现思路
         * 第1步：查询所有课程信息，在页面上以下拉框显示，供用户选择；
         * 第2步：校验当前班级号、周次、节次下是否有课程，如果有，在该记录上进行修改，如果没有则添加
         */
        List<Course> courseList = courseService.queryAllCourseList();
        model.addAttribute("courseList", courseList);

        // FIXME 需要解决bug，因为通过节次、周次不能确定唯一记录
//        CourseTime courseTime = courseTimeService.queryByCourseTime(section, week);
        CourseTime courseTime = null;
        //非空判断 如果该条记录为空 给他赋默认值
        if(null == courseTime){
            courseTime = new CourseTime();
            courseTime.setClassCode(classCode);
            courseTime.setSection(section);
            courseTime.setWeek(week);
        }
        model.addAttribute("courseTime", courseTime);
        return "/course/editCourseTime";
    }

    @GetMapping("/delete/{id}")
    public String deleteCourseTimeById(@PathVariable("id") Integer id) {
        boolean b = courseTimeService.deleteCourseTimeListById(id);
        return "redirect:/courseTime/queryByCourseTimeList";
    }

}
